﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CuoiHoi.Business;
using CuoiHoi.Entities;

namespace CuoiHoi.Admin
{
    public partial class ucQLTheLoai : System.Web.UI.UserControl
    {
        public bool isClick = false;
        static int idTheLoai = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            thutu.Visible = false;
            LoadTheLoai();
            LoadData();
        }
        void LoadData()
        {
            gvTheLoai.DataSource = BTheLoai.SelectAll();
            gvTheLoai.DataBind();
        }

        void LoadTheLoai()
        {
            ddTheLoaiCha.DataSource = BTheLoai.SelectAll();
            ddTheLoaiCha.DataTextField = "TenTheLoai";
            ddTheLoaiCha.DataValueField = "idTheLoai";
            ddTheLoaiCha.DataBind();

            ddTheLoaiCha.Items.Insert(0, new ListItem { Text = "---------Không---------", Value = "0" });
            ddTheLoaiCha.SelectedIndex = 0;
        }
        protected void gvTheLoai_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvTheLoai.PageIndex = e.NewPageIndex;
            gvTheLoai.DataBind();
        }

        void LoadThuTu(int IdTheLoai)
        {
            ddThuTu.DataSource = BTheLoai.SelectThuTu(IdTheLoai);
            ddThuTu.DataTextField = "ThuTu";
            ddThuTu.DataValueField = "ThuTu";
            ddThuTu.DataBind();
            
        }
        protected void gvTheLoai_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ChonSua")
            {
                int index = int.Parse(e.CommandArgument.ToString());
                idTheLoai = int.Parse(gvTheLoai.DataKeys[index].Values[0].ToString());

                thutu.Visible = true;
                LoadThuTu(idTheLoai);
                txtTheLoai.Text = HttpUtility.HtmlDecode(gvTheLoai.Rows[index].Cells[1].Text);
                ddKieuHienThi.SelectedValue = gvTheLoai.Rows[index].Cells[2].Text;
                ddThuTu.SelectedValue = gvTheLoai.Rows[index].Cells[3].Text;
                txtTieuDe.Text = HttpUtility.HtmlDecode(gvTheLoai.Rows[index].Cells[4].Text);
                txtMoTa.Text =HttpUtility.HtmlDecode( gvTheLoai.Rows[index].Cells[5].Text);
                txtTuKhoa.Text = HttpUtility.HtmlDecode(gvTheLoai.Rows[index].Cells[6].Text);
                ddTheLoaiCha.SelectedValue = gvTheLoai.DataKeys[index].Values[1].ToString();
                ddTrangThai.SelectedValue = gvTheLoai.Rows[index].Cells[8].Text == "True" ? "1" : "0";
                isClick = true;
            }
        }

        protected void gvTheLoai_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int index = int.Parse(e.RowIndex.ToString());
            BTheLoai.Delete(int.Parse(gvTheLoai.DataKeys[index].Values[0].ToString()));
            LoadData();
            Session["action"] = 3;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            BTheLoai.Insert(new ETheLoai
            {
                DuongDan = ddKieuHienThi.SelectedValue,
                TenTheLoai = txtTheLoai.Text,
                idTheLoaiCha = ddTheLoaiCha.SelectedValue,
                Title = txtTieuDe.Text,
                Description = txtMoTa.Text,
                Keyword = txtTuKhoa.Text,
                ThuTu = 0,
                TrangThai = ddTrangThai.SelectedValue=="1"?true:false
            });

            LoadData();
            Session["action"] = 1;
        }

        protected void btSua_Click(object sender, EventArgs e)
        {
            BTheLoai.Update(new ETheLoai
            {
                idTheLoai = idTheLoai,
                DuongDan = ddKieuHienThi.SelectedValue,
                TenTheLoai = txtTheLoai.Text,
                idTheLoaiCha = ddTheLoaiCha.SelectedValue,
                Title = txtTieuDe.Text,
                Description = txtMoTa.Text,
                Keyword = txtTuKhoa.Text,
                TrangThai = ddTrangThai.SelectedValue == "1" ? true : false
            });

            BTheLoai.UpdateThuTu(new ETheLoai
            {
                idTheLoai = idTheLoai,
                ThuTu = byte.Parse(ddThuTu.SelectedValue)
            });

            LoadData();
            Session["action"] = 2;
        }
        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>alert('" + Loi + "')</script>", false);
        }
        protected void lbXoaTat_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < gvTheLoai.Rows.Count; i++)
                {
                    CheckBox cbChon = (CheckBox)gvTheLoai.Rows[i].Cells[0].FindControl("cbChon");
                    if (cbChon != null && cbChon.Checked)
                    {
                        BTheLoai.Delete(int.Parse(gvTheLoai.DataKeys[i].Values[0].ToString()));
                    }
                }

                LoadData();
                Session["action"] = 3;
            }
            catch
            {
                ThongBao("Dữ liệu liên quan đến một số bậc lương vẫn tồn tại, vui lòng kiểm tra lại dữ liệu!");
            }
        }
    }
}